package vg.model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.almworks.sqlite4java.SQLiteException;
import com.almworks.sqlite4java.SQLiteStatement;
import vg.core.AModel;
import vg.core.VisualGraph;
import vg.core.exception.CoreException;
import vg.core.exception.EnumCriticalityException;
import vg.core.graph.ENodeType;
import vg.core.graph.Graph;
import vg.core.graph.GraphNode;
import vg.core.graph.SubGraph;
import vg.core.storableGraph.StorableAttribute;
import vg.core.storableGraph.StorableEdge;
import vg.core.storableGraph.StorableGraph;
import vg.core.storableGraph.StorableSubGraph;
import vg.core.storableGraph.StorableVertex;
/**
* This class is storage. In MVC architect, It is MODEL.
* This class uses SQLite.
* @author tzolotuhin
*/
public class SQLiteModel extends AModel {
private Connection currConnection;
//private HashMap<Integer, GraphNode> graphSkeletons;
//-------------------------------------------------------------------------
public SQLiteModel() throws CoreException {
//creating of new data base--------------
try {
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [PROCESS] Creating of database");
Class.forName("org.sqlite.JDBC");
this.currConnection = DriverManager.getConnection("jdbc:sqlite:data/db/current.db");
//this.graphSkeletons = new HashMap<Integer, GraphNode>();
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [OK] Creating of database");
} catch (Exception ex) {
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [FAIL] Creating of database. Exception : " + ex.getMessage());
throw(new CoreException("[Model.Model] [Creating of data base] Exception = " + ex.getMessage(),EnumCriticalityException.FAILED));
}
//creating of tables---------------------
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [PROCESS] Creating of tables");
Statement currStatement = null;
try {
//drop old tables if exists----------
currStatement = this.currConnection.createStatement();
currStatement.executeUpdate("drop table if exists com_graph_subgraph;");
currStatement.executeUpdate("drop table if exists com_subgraph_edge;");
currStatement.executeUpdate("drop table if exists com_subgraph_vertex;");
currStatement.executeUpdate("drop table if exists com_edge_attribute;");
currStatement.executeUpdate("drop table if exists com_vertex_attribute;");
currStatement.executeUpdate("drop table if exists graph;");
currStatement.executeUpdate("drop table if exists subgraph;");
currStatement.executeUpdate("drop table if exists attribute;");
currStatement.executeUpdate("drop table if exists vertex;");
currStatement.executeUpdate("drop table if exists edge;");
//create new tables------------------
currStatement.executeUpdate("create table vertex (db_id INTEGER PRIMARY KEY, id VARCHAR, db_id_inner_graph INTEGER);");
currStatement.executeUpdate("create table edge (db_id INTEGER PRIMARY KEY, id VARCHAR, db_id_source INT, db_id_target INT);");
currStatement.executeUpdate("create table attribute (db_id INTEGER PRIMARY KEY, name VARCHAR, value VARCHAR);");
currStatement.executeUpdate("create table subgraph (db_id INTEGER PRIMARY KEY, id VARCHAR, name VARCHAR, directed BOOLEAN);");
currStatement.executeUpdate("create table graph (db_id INTEGER PRIMARY KEY, root_key INTEGER, name VARCHAR);");
currStatement.executeUpdate("create table com_vertex_attribute (db_id INTEGER PRIMARY KEY autoincrement, db_id_vertex INTEGER, db_id_attribute INTEGER);");
currStatement.executeUpdate("create table com_edge_attribute (db_id INTEGER PRIMARY KEY autoincrement, db_id_edge INTEGER, db_id_attribute INTEGER);");
currStatement.executeUpdate("create table com_subgraph_vertex (db_id INTEGER PRIMARY KEY autoincrement, db_id_subgraph INTEGER, db_id_vertex INTEGER);");
currStatement.executeUpdate("create table com_subgraph_edge (db_id INTEGER PRIMARY KEY autoincrement, db_id_subgraph INTEGER, db_id_edge INTEGER);");
currStatement.executeUpdate("create table com_graph_subgraph (db_id INTEGER PRIMARY KEY autoincrement, db_id_graph INTEGER, db_id_subgraph INTEGER);");
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [OK] Creating of tables");
} catch (Exception ex) {
VisualGraph.log.printDebug("[" + this.getClass().getName() + ".SQLiteModel] [FAIL] Creating of tables. Exception : " + ex.getMessage());
throw(new CoreException("[" + this.getClass().getName() + ".SQLiteModel] [Creating of tables] Exception = " + ex.getMessage(),EnumCriticalityException.FAILED));
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
}
//-------------------------------------------------------------------------
public int addGraph(Graph graph) throws CoreException
{
return 0;
}
public synchronized void addStorableGraph(StorableGraph sg, ArrayList<Integer> subgraphIDs)
{
dbAddGraph(sg, subgraphIDs);
}
public synchronized void addStorableSubGraph(StorableSubGraph ssg)
{
dbAddSubGraph(ssg);
}
public synchronized void closeGraph(int number) {
}
public synchronized Graph getGraph(int number) {
StorableGraph sg = dbGetStorableGraph(number);
if(sg != null) {
return(sg.getGraph());
}
return(null);
}
public synchronized StorableGraph getStorableGraph(int number) {
return(dbGetStorableGraph(number));
}
public synchronized StorableSubGraph getStorableSubGraph(int subGraphId) {
return(dbGetStorableSubGraph(subGraphId));
}
public synchronized SubGraph getSubGraph(int subGraphId) {
StorableSubGraph sg = dbGetStorableSubGraph(subGraphId);
if(sg != null) {
return(sg.getSubGraph());
}
return(null);
}
public synchronized StorableSubGraph getRootStorableSubGraph(int graphId) {
return(dbGetStorableRootSubGraph(graphId));
}
public synchronized SubGraph getRootSubGraph(int graphId) {
StorableSubGraph sg = dbGetStorableRootSubGraph(graphId);
if(sg != null) {
return(sg.getSubGraph());
}
return null;
}
public String getGraphName(int graphId) {
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
String request = "select * " +
"from graph s1 " +
"where s1.db_id = " + (new Integer(graphId)).toString() + ";";
ResultSet resultGraph = currStatement.executeQuery(request);
if(resultGraph.next()) {
String name = resultGraph.getString(3);
return(name);
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(null);
}
/* public synchronized ResultSet executeSQLRequest(String request) throws SQLException {
Statement currStatement = null;
currStatement = this.currConnection.createStatement();
ResultSet result = currStatement.executeQuery(request);
return(result);
}
*/
public SQLiteStatement executeSQLRequest(String request) throws SQLiteException {
return(null);
}
public synchronized GraphNode getGraphSkeleton(int graphId) {
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
String request = "select s1.root_key, s1.name " +
"from graph s1 " +
"where s1.db_id = " + (new Integer(graphId)).toString() + ";";
ResultSet resultGraph = currStatement.executeQuery(request);
//-----------------------------------
if(resultGraph.next()) {
Integer rootKey = (Integer)resultGraph.getObject(1);
String name = resultGraph.getString(2);
if(rootKey != null) {
GraphNode node = new GraphNode(ENodeType.DEF_SUBGRAPH, rootKey, name);
ArrayList<GraphNode> nodes = dbBuildSubGraphSkeleton(rootKey);
for(GraphNode buf : nodes) {
node.add(buf);
}
return(node);
}
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(null);
}
@Override
public StorableEdge getStorableEdge(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public StorableVertex getStorableVertex(int index) {
// TODO Auto-generated method stub
return null;
}
//-----------------------------------------------------------------------------
//Private methods
//-----------------------------------------------------------------------------
private ArrayList<GraphNode> dbBuildSubGraphSkeleton(int subGraphId) {
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
String request = "select s1.db_id, s1.id, s1.db_id_inner_graph " +
"from vertex s1, com_subgraph_vertex s2 " +
"where s2.db_id_subgraph = " + (new Integer(subGraphId)).toString() + " and s2.db_id_vertex = s1.db_id;";
ResultSet resultSubGraph = currStatement.executeQuery(request);
//-----------------------------------
ArrayList<GraphNode>array = new ArrayList<GraphNode>();
while(resultSubGraph.next()) {
Integer dbId = resultSubGraph.getInt(1);
String id = resultSubGraph.getString(2);
Integer innerGraph = (Integer)resultSubGraph.getObject(3);
GraphNode node = new GraphNode(ENodeType.DEF_VERTEX, dbId, id);
array.add(node);
if(innerGraph != null) {
ArrayList<GraphNode> nodes = dbBuildSubGraphSkeleton(innerGraph);
for(GraphNode buf : nodes) {
node.add(buf);
}
}
}
return(array);
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(null);
}
private void dbAddGraph(StorableGraph sg, ArrayList<Integer> subgraphIDs) {
if(sg == null) return;
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
this.currConnection.setAutoCommit(false);//very big plus to work speed
//add graph--------------------------
String request = "insert into graph values(" + new Integer(sg.getStorableId()).toString();
if(sg.getRootSubGraphNumber() == null) {
request += ", -1,";
} else {
request += ", " + new Integer(sg.getRootSubGraphNumber()).toString() + ",";
}
if(sg.getName() == null) {
request += " null);";
} else {
request += "'" + sg.getName() + "');";
}
currStatement.addBatch(request);//ADD TO DATABASE
//currStatement.executeBatch();//EXECUTE
//add subgraphs----------------------
for(Integer id : subgraphIDs) {
request = "insert into com_graph_subgraph values(?, ";
request += new Integer(sg.getStorableId()).toString();
request += ", " + new Integer(id.toString());
request += ");";
currStatement.addBatch(request);//ADD TO DATABASE
}
currStatement.executeBatch();//EXECUTE
this.currConnection.setAutoCommit(true);
} catch (Exception ex) {
VisualGraph.log.printException(ex);
VisualGraph.windowMessage.warningMessage(ex.getMessage(), "Adding to database");
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
}
private void dbAddSubGraph(StorableSubGraph ssg)
{
if(ssg == null) return;
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
this.currConnection.setAutoCommit(false);//very big plus to work speed
//add graph--------------------------
StringBuffer request = new StringBuffer(10*1024);
request.append("insert into subgraph values(");
request.append(ssg.getStorableId());
if(ssg.getId() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(ssg.getId());
request.append("'");
}
if(ssg.getName() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(ssg.getName());
request.append("'");
}
if(ssg.isDirected()) {
request.append(", 'true');");
} else {
request.append(", 'false');");
}
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
currStatement.executeBatch();
//add all vertexes---------------
for(StorableVertex bufVertex : ssg.getVertexes()) {
request.append("insert into vertex values(");
request.append(bufVertex.getStorableId());
if(bufVertex.getId() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(bufVertex.getId());
request.append("'");
}
if(bufVertex.getInnerGraph() == null) {
request.append(", null);");
} else {
request.append(", ");
request.append(bufVertex.getInnerGraph());
request.append(");");
}
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
//add all attributes---------
for(StorableAttribute bufAttribute : bufVertex.getAttributes()) {
request.append("insert into attribute values(");
request.append(bufAttribute.getStorableId());
if(bufAttribute.getName() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(bufAttribute.getName());
request.append("'");
}
if(bufAttribute.getValue() == null) {
request.append(", null);");
} else {
request.append(", '");
request.append(bufAttribute.getValue());
request.append("');");
}
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
//add composition between vertex and attribute
request.append("insert into com_vertex_attribute values(?, ");
request.append(bufVertex.getStorableId());
request.append(", ");
request.append(bufAttribute.getStorableId());
request.append(");");
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
}
//add composition between subgraph and vertex
request.append("insert into com_subgraph_vertex values(?, ");
request.append(ssg.getStorableId());
request.append(", ");
request.append(bufVertex.getStorableId());
request.append(");");
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
currStatement.executeBatch();
}
//add all edges------------------
for(StorableEdge bufEdge : ssg.getEdges()) {
request.append("insert into edge values(");
request.append(bufEdge.getStorableId());
if(bufEdge.getId() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(bufEdge.getId());
request.append("'");
}
request.append(", ");
request.append(bufEdge.getStorableSource().getStorableId());
request.append(", ");
request.append(bufEdge.getStorableTarget().getStorableId());
request.append(");");
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
//add all attributes---------
for(StorableAttribute bufAttribute : bufEdge.getStorableAttributes()) {
request.append("insert into attribute values(");
request.append(bufAttribute.getStorableId());
if(bufAttribute.getName() == null) {
request.append(", null");
} else {
request.append(", '");
request.append(bufAttribute.getName());
request.append("'");
}
if(bufAttribute.getValue() == null) {
request.append(", null);");
} else {
request.append(", '");
request.append(bufAttribute.getValue());
request.append("');");
}
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
//add composition between edge and attribute
request.append("insert into com_edge_attribute values(?, ");
request.append(bufEdge.getStorableId());
request.append(", ");
request.append(bufAttribute.getStorableId());
request.append(");");
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
}
//add composition between subgraph and edge
request.append("insert into com_subgraph_edge values(?, ");
request.append(ssg.getStorableId());
request.append(", ");
request.append(bufEdge.getStorableId());
request.append(");");
currStatement.addBatch(request.toString());//ADD TO DATABASE
request.setLength(0);
currStatement.executeBatch();
}
this.currConnection.setAutoCommit(true);
} catch (Exception ex) {
VisualGraph.log.printException(ex);
VisualGraph.windowMessage.warningMessage(ex.getMessage(), "Adding to database");
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
}
private StorableGraph dbGetStorableGraph(int number) {
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
String request = "select * " +
"from com_graph_subgraph s1 " +
"where s1.db_id_graph = " + (new Integer(number)).toString() + ";";
ResultSet resultComSubGraph = currStatement.executeQuery(request);
StorableGraph sg = new StorableGraph(number);
while(resultComSubGraph.next()) {
//getting of subgraph------------
int id_subgraph = resultComSubGraph.getInt(3);
boolean directed = false;
String id = null;
String name = null;
Statement subgraphStatement = null;
try {
subgraphStatement = this.currConnection.createStatement();
request = "select *" +
"from subgraph s1 " +
"where s1.db_id = " + (new Integer(id_subgraph)).toString() + ";";
ResultSet resultSubGraph = subgraphStatement.executeQuery(request);
if(resultSubGraph.next()) {
String buf = resultSubGraph.getString(4);
if(buf != null && buf.equals("true")) {
directed = true;
} else {
directed = false;
}
id = resultSubGraph.getString(2);
name = resultSubGraph.getString(3);
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(subgraphStatement != null) {
try {
subgraphStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//vertexes-----------------------
ArrayList<StorableVertex>listVertex = new ArrayList<StorableVertex>();
Statement vertexStatement = null;
try {
vertexStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.id, s2.db_id_inner_graph " +
"from com_subgraph_vertex s1, vertex s2 " +
"where s1.db_id_subgraph = " + (new Integer(id_subgraph)).toString() + " and s2.db_id = s1.db_id_vertex;";
ResultSet resultVertex = vertexStatement.executeQuery(request);
while(resultVertex.next()) {
int db_id_vertex = resultVertex.getInt(1);
String id_vertex = resultVertex.getString(2);
Integer db_id_innder_graph = (Integer)resultVertex.getObject(3);
StorableVertex v = new StorableVertex(db_id_vertex, id_vertex);
v.setInnerGraph(db_id_innder_graph);
listVertex.add(v);
//attributes-----------------
Statement attrStatement = null;
try {
attrStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.name, s2.value " +
"from com_vertex_attribute s1, attribute s2 " +
"where s1.db_id_vertex = " + (new Integer(db_id_vertex)).toString() + " and s2.db_id = s1.db_id_attribute;";
ResultSet resultAttribute = attrStatement.executeQuery(request);
while(resultAttribute.next()) {
int db_id_attr = resultAttribute.getInt(1);
String db_name = resultAttribute.getString(2);
String db_value = resultAttribute.getString(3);
v.addStorableAttribute(new StorableAttribute(db_id_attr, db_name, db_value));
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(attrStatement != null) {
try {
attrStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(vertexStatement != null) {
try {
vertexStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//edges--------------------------
ArrayList<StorableEdge>listEdge = new ArrayList<StorableEdge>();
Statement edgeStatement = null;
try {
edgeStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.id, s2.db_id_source, db_id_target " +
"from com_subgraph_edge s1, edge s2 " +
"where s1.db_id_subgraph = " + (new Integer(id_subgraph)).toString() + " and s2.db_id = s1.db_id_edge;";
ResultSet resultEdge = edgeStatement.executeQuery(request);
while(resultEdge.next()) {
int db_id_edge = resultEdge.getInt(1);
String id_edge = resultEdge.getString(2);
Integer db_id_source = (Integer)resultEdge.getObject(3);
Integer db_id_target = (Integer)resultEdge.getObject(4);
if(db_id_source != null && db_id_target != null) {
StorableVertex source = null, target = null;
for(StorableVertex bufVertex : listVertex) {
if(bufVertex.getStorableId() == db_id_source) {
source = bufVertex;
}
if(bufVertex.getStorableId() == db_id_target) {
target = bufVertex;
}
}
if(source != null && target != null) {
StorableEdge e = new StorableEdge(db_id_edge, source, target, id_edge);
listEdge.add(e);
//attributes-----------------
Statement attrStatement = null;
try {
attrStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.name, s2.value " +
"from com_edge_attribute s1, attribute s2 " +
"where s1.db_id_edge = " + (new Integer(db_id_edge)).toString() + " and s2.db_id = s1.db_id_attribute;";
ResultSet resultAttribute = attrStatement.executeQuery(request);
while(resultAttribute.next()) {
int db_id_attr = resultAttribute.getInt(1);
String db_name = resultAttribute.getString(2);
String db_value = resultAttribute.getString(3);
e.addStorableAttribute(new StorableAttribute(db_id_attr, db_name, db_value));
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(attrStatement != null) {
try {
attrStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
} else {
VisualGraph.log.printError("[" + this.getClass().getName() + ".getGraph] [BAD] Source edge = null || target edge = null.(" + db_id_source + "," + db_id_target + ")");
}
}
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(edgeStatement != null) {
try {
edgeStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//build subgraph-----------------
StorableSubGraph ssg = new StorableSubGraph(id_subgraph, id, name, listVertex, listEdge, directed);
sg.addSubGraph(ssg);
}
//getting of graph-------------------
Statement graphStatement = null;
try {
graphStatement = this.currConnection.createStatement();
request = "select * " +
"from graph s1 " +
"where s1.db_id = " + (new Integer(number)).toString() + ";";
ResultSet resultGraph = currStatement.executeQuery(request);
if(resultGraph.next()) {
Integer rootKey = resultGraph.getInt(2);
String name = resultGraph.getString(3);
if(rootKey != null) {
sg.setRoot(rootKey);
}
if(name != null) {
sg.setName(name);
}
}
} catch (Exception ex) {
VisualGraph.log.printException(ex);
} finally {
if(graphStatement != null) {
try {
graphStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(sg);
} catch (Exception ex) {
VisualGraph.log.printError("[" + this.getClass().getName() + ".getGraph] [BAD] Getting of graph. Exception : " + ex.getMessage());
VisualGraph.log.printException(ex);
VisualGraph.windowMessage.warningMessage("Exception : " + ex.getMessage(), "Get graph");
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(null);
}
private StorableSubGraph dbGetStorableRootSubGraph(int graphId) {
Statement currStatement = null;
try {
currStatement = this.currConnection.createStatement();
String request = "select * " +
"from graph s1 " +
"where s1.db_id = " + (new Integer(graphId)).toString() + ";";
ResultSet resultGraph = currStatement.executeQuery(request);
if(resultGraph.next()) {
Integer rootKey = resultGraph.getInt(2);
if(rootKey != null) {
StorableSubGraph sg = dbGetStorableSubGraph(rootKey);
return(sg);
}
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(currStatement != null) {
try {
currStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
return(null);
}
/**
* This method returns storable subgraph.
*/
private StorableSubGraph dbGetStorableSubGraph(int subGraphId) {
//getting of subgraph------------
int id_subgraph = subGraphId;
String request = "";
boolean directed = false;
String id = null;
String name = null;
Statement subgraphStatement = null;
try {
subgraphStatement = this.currConnection.createStatement();
request = "select *" +
"from subgraph s1 " +
"where s1.db_id = " + (new Integer(id_subgraph)).toString() + ";";
ResultSet resultSubGraph = subgraphStatement.executeQuery(request);
if(resultSubGraph.next()) {
String buf = resultSubGraph.getString(4);
if(buf != null && buf.equals("true")) {
directed = true;
} else {
directed = false;
}
id = resultSubGraph.getString(2);
name = resultSubGraph.getString(3);
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(subgraphStatement != null) {
try {
subgraphStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//vertexes-----------------------
ArrayList<StorableVertex>listVertex = new ArrayList<StorableVertex>();
Statement vertexStatement = null;
try {
vertexStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.id, s2.db_id_inner_graph " +
"from com_subgraph_vertex s1, vertex s2 " +
"where s1.db_id_subgraph = " + (new Integer(id_subgraph)).toString() + " and s2.db_id = s1.db_id_vertex;";
ResultSet resultVertex = vertexStatement.executeQuery(request);
while(resultVertex.next()) {
int db_id_vertex = resultVertex.getInt(1);
String id_vertex = resultVertex.getString(2);
Integer db_id_innder_graph = (Integer)resultVertex.getObject(3);
StorableVertex v = new StorableVertex(db_id_vertex, id_vertex);
v.setInnerGraph(db_id_innder_graph);
listVertex.add(v);
//attributes-----------------
Statement attrStatement = null;
try {
attrStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.name, s2.value " +
"from com_vertex_attribute s1, attribute s2 " +
"where s1.db_id_vertex = " + (new Integer(db_id_vertex)).toString() + " and s2.db_id = s1.db_id_attribute;";
ResultSet resultAttribute = attrStatement.executeQuery(request);
while(resultAttribute.next()) {
int db_id_attr = resultAttribute.getInt(1);
String db_name = resultAttribute.getString(2);
String db_value = resultAttribute.getString(3);
v.addStorableAttribute(new StorableAttribute(db_id_attr, db_name, db_value));
}
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(attrStatement != null) {
try {
attrStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(vertexStatement != null) {
try {
vertexStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//edges--------------------------
ArrayList<StorableEdge>listEdge = new ArrayList<StorableEdge>();
Statement edgeStatement = null;
try {
edgeStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.id, s2.db_id_source, db_id_target " +
"from com_subgraph_edge s1, edge s2 " +
"where s1.db_id_subgraph = " + (new Integer(id_subgraph)).toString() + " and s2.db_id = s1.db_id_edge;";
ResultSet resultEdge = edgeStatement.executeQuery(request);
while(resultEdge.next()) {
int db_id_edge = resultEdge.getInt(1);
String id_edge = resultEdge.getString(2);
Integer db_id_source = (Integer)resultEdge.getObject(3);
Integer db_id_target = (Integer)resultEdge.getObject(4);
if(db_id_source != null && db_id_target != null) {
StorableVertex source = null, target = null;
for(StorableVertex bufVertex : listVertex) {
if(bufVertex.getStorableId() == db_id_source) {
source = bufVertex;
}
if(bufVertex.getStorableId() == db_id_target) {
target = bufVertex;
}
}
if(source != null && target != null) {
StorableEdge e = new StorableEdge(db_id_edge, source, target, id_edge);
listEdge.add(e);
//attributes-----------------
Statement attrStatement = null;
try {
attrStatement = this.currConnection.createStatement();
request = "select s2.db_id, s2.name, s2.value " +
"from com_edge_attribute s1, attribute s2 " +
"where s1.db_id_edge = " + (new Integer(db_id_edge)).toString() + " and s2.db_id = s1.db_id_attribute;";
ResultSet resultAttribute = attrStatement.executeQuery(request);
while(resultAttribute.next()) {
int db_id_attr = resultAttribute.getInt(1);
String db_name = resultAttribute.getString(2);
String db_value = resultAttribute.getString(3);
e.addStorableAttribute(new StorableAttribute(db_id_attr, db_name, db_value));
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(attrStatement != null) {
try {
attrStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
} else {
VisualGraph.log.printError("[" + this.getClass().getName() + ".getGraph] [BAD] Source edge = null || target edge = null.(" + db_id_source + "," + db_id_target + ")");
}
}
}
} catch (SQLException ex) {
VisualGraph.log.printException(ex);
} finally {
if(edgeStatement != null) {
try {
edgeStatement.close();
} catch(SQLException ex) {
VisualGraph.log.printException(ex);
}
}
}
//build subgraph-----------------
StorableSubGraph ssg = new StorableSubGraph(id_subgraph, id, name, listVertex, listEdge, directed);
return(ssg);
}
}